<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Module_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'asc', $current_page = 0){
			$search='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search = $array;
					}
				}
						
			$results_per_page = 3;
		
		
			$url = 'http://localhost/azindia/azindia-api/json.api';
			$data = array('www-command'=>'usermanagement-all_modules', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by,'search_by'=>'module_name','search_by_value'=>$search);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			if($response->message !== 'No modules'){
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
		
			$this->load->library('pagination');			
			$config['base_url'] = 'http://localhost/azindia/azindia-client/index.php/admin/module_ctrl/index/'.$query_id.'/'. $order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['usermanagement_all_modules'] = $response;
			$data['module'] = '';
			}else{
				$data['module'] = 1;
				$data['usermanagement_all_modules'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['module_fileds'] = array('module_name'=>'Name', 'sequence'=>'Sequence', 'created_date'=>'Date');
			$data['sort_order']=$sort_order;
			$data['order_by']=$order_by;
			$data['query_id']=$query_id;
			$data['search']=$search;
			$this->load->view('admin/module_view', $data);
 		}
		
		function search_by(){
			$query_id = rand();
			$_SESSION['search'] = $this->input->post('search');
			redirect('admin/module_ctrl/index/'.$query_id);
		}
		
		/* function create_module_form(){
			$this->load->view('admin/create_module_form');
		} */
		
		function create_module(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required|alpha|min_length[3]|max_length[20]');
				$this->form_validation->set_rules('sequence', 'Sequence', 'required|numeric|max_length[10]');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/create_module_form');
				}else{
					$url = CURL_URL;
					$data = array('www-command'=>'usermanagement-create_module', 
					'module_name' => $this->input->post('name'),
				'sequence' => $this->input->post('sequence')
				);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/module_ctrl/index/');
				}
			}
			
		/* 	function edit_module_form(){
			 $url = CURL_URL;				
				$data = array('www-command'=>'usermanagement-get_module','module_id'=>$this->uri->segment(4));
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				$response = (array) $response;
				foreach($response as $value){
					$value = $value;
				}
				//echo '<pre>';print_r($value);exit; 
				
			$data = array('www-command'=>'usermanagement-all_submodules');
			//echo '<pre>';print_r($data);exit;
			$string = http_build_query($data);
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			$data['usermanagement_all_submodules'] = $response;
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
				$data['name'] = $value[0]->module_name;
				$data['sequence'] = $value[0]->sequence;  
				$data['module_id'] = $value[0]->module_id; 
				$this->load->view('admin/edit_module_form', $data);
			} */
			
			function edit_module(){
				
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required|alpha|min_length[3]|max_length[20]');
				$this->form_validation->set_rules('sequence', 'Sequence', 'required|numeric|max_length[10]');
				
				if ($this->form_validation->run() == FALSE)
			{
				$url = CURL_URL;
				if($this->uri->segment(4) > 0){
					$_SESSION['module_id'] = $this->uri->segment(4);
				}
				$data = array('www-command'=>'usermanagement-get_module','module_id'=>$_SESSION['module_id']);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				$response = (array) $response;
				foreach($response as $value){
					$value = $value;
				}
				//echo '<pre>';print_r($value);exit; 
				
				$data = array('www-command'=>'usermanagement-all_submodules');
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response= json_decode(curl_exec($ch));
				$data['usermanagement_all_submodules'] = $response;
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				$data['name'] = $value[0]->module_name;
				$data['sequence'] = $value[0]->sequence;
				$data['module_id'] = $value[0]->module_id; 
				$this->load->view('admin/edit_module_form', $data);
				
			}else{
				$url = 'http://localhost/azindia/azindia-api/json.api';				
				$data = array('www-command'=>'usermanagement-edit_module','module_name'=>$this->input->post('name'),'sequence'=>$this->input->post('sequence'),'module_id'=>$_SESSION['module_id']);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				redirect('admin/module_ctrl/index');
			}
		}
			
			function delete_module(){
				$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'usermanagement-delete_module','status'=>$status,'module_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/module_ctrl/index/');

			}
			
			/* function submodule_form(){
				$this->load->view('admin/submodule_form');
			} */
			
			function create_submodule(){
				 $this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('submodule', 'Submodule', 'required|alpha|max_length[30]|min_length[3]');
				$this->form_validation->set_rules('file_name', 'Filename', 'required|max_length[30]');
				$this->form_validation->set_rules('sequence', 'Sequence', 'required|numeric');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/submodule_form');
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'usermanagement-create_submodule', 
					'module_name' => $this->input->post('submodule'),
				'sequence' => $this->input->post('sequence'),
				'file_name' => $this->input->post('file_name'),
				'module_id' => $_SESSION['module_id']
				);
				$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/module_ctrl/edit_module/');
				} 
			}
			
			/* function edit_submodule_form(){
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'usermanagement-get_submodule','module_id'=>$this->uri->segment(4));
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
					$response = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					$response = (array) $response;
					foreach($response as $value){
						$value = $value;
					}
					//echo '<pre>';print_r($response);exit;
					$data['module_name'] = $value[0]->module_name;
					$data['file_name'] = $value[0]->file_name;
					$data['sequence'] = $value[0]->sequence;
					//echo '<pre>';print_r($data);exit;
					$this->load->view('admin/edit_submodule_form', $data);
			} */
			
			function edit_submodule(){
					$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('submodule', 'Submodule', 'required|alpha|max_length[30]|min_length[3]');
				$this->form_validation->set_rules('file_name', 'Filename', 'required|max_length[30]');
				$this->form_validation->set_rules('sequence', 'Sequence', 'required|numeric');
				
				if ($this->form_validation->run() == FALSE)
			{
				$url = CURL_URL;
				if($this->uri->segment(4) > 0){
					$_SESSION['sub_id'] = $this->uri->segment(4);
				}
					$data = array('www-command'=>'usermanagement-get_submodule','module_id'=>$_SESSION['sub_id']);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
					$response = json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					$response = (array) $response;
					foreach($response as $value){
						$value = $value;
					}
					//echo '<pre>';print_r($response);exit;
					$data['module_name'] = $value[0]->module_name;
					$data['file_name'] = $value[0]->file_name;
					$data['sequence'] = $value[0]->sequence;
					//echo '<pre>';print_r($data);exit;
					$this->load->view('admin/edit_submodule_form', $data);  
				
				
			}else{
				
				$url = CURL_URL;			
				$data = array('www-command'=>'usermanagement-edit_submodule','module_name'=>$this->input->post('submodule'),'file_name'=>$this->input->post('file_name'),'sequence'=>$this->input->post('sequence'),'module_id'=>$_SESSION['sub_id']);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				redirect('admin/module_ctrl/edit_module/');
			}
			}
			
			function delete_submodule(){
				$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'usermanagement-delete_submodule','status'=>$status, 'module_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/module_ctrl/edit_module/');

			}
			
			
			
	}
		
?>